package org.iteam.xlz.manager.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.iteam.commons.security.sha.ShaUtil;
import org.iteam.core.exception.NoPageException;
import org.iteam.web.kaptcha.Constants;
import org.iteam.web.struts.action.JsonServletAction;

/**
 * 登录action
 * 
 */
@Results({@Result(name = "LOGIN", type = "freemarker", location = "/WEB-INF/content/login.html"),
		  @Result(name="REDI",type="redirect",location="main")})
public class LoginAction extends JsonServletAction {
	
	
	private static final long serialVersionUID = 1986219101798636188L;

	/**
	 * 登录页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/main", interceptorRefs = @InterceptorRef("defaultStack"))
	public String main() throws Exception {
		return "LOGIN";
	}

	/**
	 * 404处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/pageNotFund", interceptorRefs = @InterceptorRef("defaultStack"))
	public String pageNotFund() throws Exception {
		throw new NoPageException("您访问的页面不存在！");
	}

	/**
	 * 登录处理
	 * 
	 * @return
	 */
	@Action(value = "/login", interceptorRefs = @InterceptorRef("defaultStack"))
	public String login() {
		try {
			// 获取用户实体
			Subject currentUser = SecurityUtils.getSubject();
			UsernamePasswordToken token = new UsernamePasswordToken(account, ShaUtil.toSha256String(pwd));
			currentUser.login(token);
			String kaptcha = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
			if (code == null || !code.equalsIgnoreCase(kaptcha)) {
				setResult(false, "验证码错误");
			} else {
				setResult(true, "成功");
			}
		} catch (UnknownAccountException uae) {
			setResult(false, "用户名无效");
		} catch (IncorrectCredentialsException ice) {
			setResult(false, "密码无效");
		} catch (LockedAccountException lae) {
			setResult(false, "帐号锁定");
		} catch (AuthenticationException ae) {
			setResult(false, "认证未通过，请输入正确的用户名和密码");
		} catch (Exception e) {
			setResult(false, "认证未通过，发生异常");
		}
		return MAP;
	}

	/**
	 * 退出
	 * 
	 * @return
	 */
	@Action(value = "/logout", interceptorRefs = @InterceptorRef("defaultStack"))
	public String logout() {
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
		return "REDI";
	}
	
	/**
	 * 账号
	 */
	private String account;
	/**
	 * 123456密码：49dc52e6bf2abe5ef6e2bb5b0f1ee2d765b922ae6cc8b95d39dc06c21c848f8c
	 */
	private String pwd;
	/**
	 * 验证码
	 */
	private String code;

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
